<?php

namespace App\Http\Controllers;
use App\Http\Controllers\HttpClient;
use App\Models\Admin;
use Illuminate\Http\Request;

use App\Models\Income;
use App\Models\User;
use Excel;
use DB;
use HttpRequest;
use Curl;

class HomeController extends Controller
{

    /**
     * 开户页
     */


    public function index($code = 'basic')
    {
        $data = Admin::where("code", $code)->where('act', '1')->count();
        if ($data) {

            $url = 'https://lp.fxclub.cn/api/1/security/captcha/?callback=jQuery111209433767567777742_1492001292383&_='.time();
            $output=$this->postcurl($url);
//打印获得的数据
            $output = $this->getNeedBetween($output,"(",")");
            $output = json_decode($output, true);
//            dd($output);
            $response = isset($output['captcha_response'])?$output['captcha_response']:"";//二维码

                return view("info.open",[
                    'captcha_response' => $response,
                    'output' => $output,
                    'code' => $code
                ]);

        } else {
            return view("errors.link");
        }


    }
    /**
     * 手机端开户页
     */


    public function index2($code = 'basic')
    {
        $data = Admin::where("code", $code)->where('act', '1')->count();
        if ($data) {

            $url = 'https://lp.fxclub.cn/api/1/security/captcha/?callback=jQuery111209433767567777742_1492001292383&_='.time();
            $output=$this->postcurl($url);
//打印获得的数据
            $output = $this->getNeedBetween($output,"(",")");
            $output = json_decode($output, true);
//            dd($output);
            $response = isset($output['captcha_response'])?$output['captcha_response']:"";//二维码

            return view("mobile.open",[
                'captcha_response' => $response,
                'output' => $output,
                'code' => $code
            ]);

        } else {
            return view("errors.link");
        }


    }

    public function getNeedBetween($kw1,$mark1,$mark2){
        $kw=$kw1;
        $st =stripos($kw,$mark1);
        $ed =stripos($kw,$mark2);
        if(($st==false||$ed==false)||$st>=$ed)
            return 0;
        $kw=substr($kw,($st+1),($ed-$st-1));
        return $kw;
    }

    public function store(Request $request)
    {
        if($request->vcode ==$_SESSION["code"]){
            unset($_SESSION["code"]);
        $email = $request->email;
        $rel = $this->check_email($email);
        if($rel){
//            dd(session('code'),$request->vcode);

            $email = $request->email;
            $password = $request->password;
            $firstname = $request->firstname;
            $lastname = $request->lastname;
            $country = $request->country;
            $city = $request->city;
            $phone = $request->phone;
            $day = $request->day;
            $month = $request->month;
            $year = $request->year;
            $birth = $year.'-'.$month.'-'.$day.' 00:00:00';
            $code =$request->code;

            $num = Admin::where('email',$email)->count();
            if($num > 0){
                $act = 1;
            }else{
                $act = 0;
            }
            $arr=User::getcountry();
            $user = new User();
            $user->email = $email;
            $user->password = $password;
            $user->lastname = $firstname;
            $user->firstname = $lastname;
            $user->country = $arr[$country];
            $user->city = $city;
            $user->phone = $phone;
            $user->birth = $birth;
            $user->code = $code;
            $user->act = $act;
            $is = $user->save();
            if($is){

                $this->setlog(($user),$lastname.$firstname.'注册账号');

                echo 1;
            }

        }else{
            echo 2;
        }
        }else{
            echo 3;
        }
    }



    public function check_email($email)
    {

        $url = 'https://lp.fxclub.cn/forex_landing_fxbank_check_email_ajax_2/?nid=55810&email='.$email.'&acctype=none&_=1492080587245';

        $data = file_get_contents($url);

        $is = '';
        if($data == 'false'){
            $count = User::where('email',$email)->count();
            if($count>0){
                $is = false;
            }else{
                $is = true;
            }
        }else{
            $is = false;
        }



        return $is;
//
//        $post_data = $request->except('_token');
//   dump($post_data);
//        $link = 'https://lp.fxclub.cn/open-account/trade/meta_market/?_ga=1.215081815.1503948664.1492051342';
//        $ch = curl_init();
//        curl_setopt($ch, CURLOPT_URL, $link);
//
//
////        curl_setopt($ch, CURLOPT_REFERER, $url);
//
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch, CURLOPT_HEADER, 0);
////        curl_setopt($ch, CURLOPT_TIMEOUT, 3); //定义超时3秒钟
////        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//头
//
//
//
//        curl_setopt($ch, CURLOPT_POST, 1);
////        curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
//        $output = curl_exec($ch);
//
//        print_r($output);
    }

    public function postcurl($request_url,$data = array()){

        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL,$request_url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec ($ch);
        curl_close($ch);
        return $result;
    }

    public function setlog($data,$content){

        $ip= $_SERVER["SERVER_ADDR"];
        $insert=array(
            'uid'=>$data->id,
            'username'=>$data->firstname.$data->lastname,
            'ip'=>$ip,
            'type'=>'2',
            'content'=>$content,
            'data'=>json_encode($data),
            'created_at'=>date("Y-m-d H:i:s")
        );
        DB::table("action_log")->insert($insert);
    }


    public function sendsms(Request $request){

//        echo 3;die;


        if(!empty($request->phone)){

        $pattern='1234567890';
        $key='';
        for($i=0;$i<6;$i++)
        {
             $key .= $pattern{mt_rand(0,9)};    //生成php随机数
        }
        $_SESSION["code"]=$key;

        include public_path("/sms/TopSdk.php");
        date_default_timezone_set('Asia/Shanghai');

        $code = '{"code":"'.$key.'","product":"福瑞斯"}';

        $c = new \TopClient();
        $c ->appkey = config("app.dayukey") ;
        $c ->secretKey = config("app.dayusecretKey")  ;
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "代理系统" );
        $req ->setSmsParam( $code);
        $req ->setRecNum( $request->phone);
        $req ->setSmsTemplateCode( "SMS_62615182" );
        $resp = $c ->execute( $req );


        $rest = $resp->result;

        if($rest->success == true){
            echo 1;
        }else{
            echo 3;
        }

        }else{
            echo 2;
        }


    }

}
